package org.eerbaar.parser.cky;

import java.util.ArrayList;
import java.util.List;

import org.eerbaar.parser.core.def.Symbol;
import org.eerbaar.parser.core.exe.HasSymbol;
import org.eerbaar.parser.core.exe.SymbolInstance;

class SymbolArray
    implements HasSymbol {

    private final List<SymbolInstance> symbols = new ArrayList<SymbolInstance>();
    private final SymbolInstance inputSymbol;

    public SymbolArray(SymbolInstance inputSymbol) {
        this.inputSymbol = inputSymbol;
    }

    public SymbolInstance getInputSymbol() {
        return inputSymbol;
    }

    public SymbolInstance getSymbol(Symbol symbol) {
        return symbols.get(indexOf(symbol));
    }

    public boolean hasSymbol(Symbol symbol) {
        return (indexOf(symbol) != -1);
    }

    public int indexOf(Symbol symbol) {

        int result = -1;

        int index = 0;
        for (Symbol s : symbols) {
            if (s.getName().equals(symbol.getName())) {
                result = index;
                break;
            }
            index++;
        }

        return result;
    }

    @Override
    public SymbolInstance next() {
        SymbolInstance result = null;
        for (Symbol s : symbols) {
            if (s instanceof SymbolInstance) {
                result = (SymbolInstance) s;
                break;
            }
        }
        return result;
    }

    @Override
    public void remove(SymbolInstance symbol) {
        symbols.remove(symbol);
    }

    public void setSymbol(SymbolInstance symbol) {
        if (!symbols.contains(symbol)) {
            symbols.add(symbol);
        }
    }

    @Override
    public String toString() {
        String s = "";
        for (Symbol ss : symbols) {
            s += ss.toDebugString() + " ";
        }
        return new Util().pad(s);
    }

}
